package handin05;

import java.util.Scanner;

/**
 * MagicSquareRunner privies an interface for MagicSquare
 * You can add numbers to MagicSquare, test or reset the
 * square.
 *  
 * @author Peter Tolstrup Aagesen, ptaa@itu.dk *
 */
public class MagicSquareRunner {

	public static void main(String[] args) {
		//Create a MagicSquare object
		MagicSquare magicSquare = new MagicSquare();
		
		//Create scanner
		Scanner in = new Scanner(System.in);
		
		//Create a variable for input
		String input = "";
		
		//Continue until the user inputs an "e"
		while (!input.equalsIgnoreCase("e"))
		{
			//Prompt user for input
			System.out.println("Square:");
			System.out.println(magicSquare.toString());
			System.out.println("Enter number to add number to square, t to test square, r to reset or e to exit:");
			
			//Set input
			input = in.next();
			
			//If "t" is inputed test square
			if(input.equalsIgnoreCase("t")){
				if(magicSquare.isMagic()) {
					System.out.print("Yay you did it! The square is magic. ");
					magicSquare.clearSquare();
					System.out.println("Try again to prove how awesome you are!");
				} else {
					System.out.print("The quare isn't magic. ");
					if(!magicSquare.isCorrectNumberSequence()){
						System.out.print("The number input is wrong. The numbers should be 1,2,3,...,n. ");
					} else if (!magicSquare.isSquare()) {
						System.out.print("More numbers are needed to form a square. ");
					} 
					System.out.println("Reset or enter more numbers.");				
				}
			//Reset square
			} else if(input.equalsIgnoreCase("r")){
				magicSquare.clearSquare();			
			} else if(!input.isEmpty()) {
				//Parse input
				try{
					int inputNum = Integer.parseInt(input);
					magicSquare.add(inputNum);					
				} catch (NumberFormatException e) {
					System.out.println("Wrong input, try again!");
				}
			}
			System.out.println();
		}		

	}

}
